package com.njlime.member.repository.sms;

import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import com.njlime.member.entiies.TField;
import com.njlime.member.entiies.TMember;
import com.njlime.member.entiies.TMemberGroup;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-7-18.
 */
@Repository
public class MemberGroupDaoImpl extends BaseDaoImpl<TMemberGroup> {
    public void editGroup(String id, int name, int unitId){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("delete from  t_member_group ");

        sb.append(" where id = ? and unit_id = ?");
        arrList.add(name);
        arrList.add(id);
        arrList.add(unitId);
        updateJdbc(sb.toString(), arrList);
    }
    /**
     * 获取列表
     *
     * @param member
     * @return
     */
    public List<TMemberGroup> list(TMemberGroup member) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.*  from t_member_group t ");

        sb.append("where t.unit_id = ? ");

        arrList.add(member.getUnit_id());

        return findList(sb.toString(), arrList);
    }

    /**
     * 获取最大的gourpid
     *
     * @param unitId
     * @return
     */
    public TMemberGroup getMaxGroupId(int unitId) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select  * from t_member_group t ");

        sb.append( "  where group_id = (select max(group_id) group_id from t_member_group where unit_id = ?) " +
                " and unit_id = ? limit 1 ");

        arrList.add(unitId);
        arrList.add(unitId);
        TMemberGroup info =  findOne(sb.toString(), arrList);
        return info ;
    }

    /**
     * 得到总人数
     * @param info
     * @return
     */
    public Map getSumMember(TMemberGroup info){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select  sum(count) count from t_member_group t ");

        sb.append( "  where   unit_id = ?  ");

        arrList.add(info.getUnit_id());
        List<Map<String, Object>>  list =  getListJdbc(sb.toString(), arrList);
        if(list != null && list.size()>0){
            return list.get(0);
        }
        return null ;
    }

    /**
     * 更新
     *
     * @param info 频道内容
     * @return
     */
    public void edit(TMemberGroup info) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("UPDATE t_member_group set name =   ?");
        sb.append(" where id= ?  and unit_id = ?");

        arrList.add(info.getName());
        arrList.add(info.getId());
        arrList.add(info.getUnit_id());
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 减少分组下的数量
     * @param imageGroupId
     */
    public void downCount(Integer imageGroupId,int unitId){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("UPDATE t_member_group set count =if(count = 0,0,count -1)  ");
        sb.append(" where id= ?  and unit_id = ?");

        arrList.add(imageGroupId);
        arrList.add(unitId);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 增加分组下的数量
     * @param imageGroupId
     */
    public void upCount(Integer imageGroupId,int unitId){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("UPDATE t_member_group set count = count + 1");
        sb.append(" where id= ?  and unit_id = ?");

        arrList.add(imageGroupId);
        arrList.add(unitId);
        updateJdbc(sb.toString(), arrList);
    }

    public void deleteGroup(String id,  int unitId){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("delete from  t_member_group ");

        sb.append(" where id = ? and unit_id = ?");
        arrList.add(id);
        arrList.add(unitId);
        updateJdbc(sb.toString(), arrList);
    }
}
